<template>
  <div class="bg">
    <!-- nav -->
    <Nav @initData="initData" />
    <!-- main -->
    <div style="display: flex">
      <Left_menu :HosData="HosList" @changeCurId="handleHosId"></Left_menu>
      <div
        class="main"
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.3)"
      >
        <!-- top -->
        <div class="main_top">
          <div class="outer">
            <ul>
              <li
                ref="tagitems"
                :class="curEquipIndex == i ? 'allInOneList2' : 'allInOneList1'"
                v-for="(item, i) in equipList"
                :key="item.id"
                @click="getCurEquip(i, item)"
              >
                {{ item.name }}
              </li>
            </ul>
          </div>
          <transition-group name="slide-fade">
            <div class="ys_box" :key="index" v-show="curEquipIndex == index" v-for="(equip, index) of equipList">
              <div class="ysList" v-for="(lable, i) of equip.allinoneData.labels" :key="i" v-show="i < 6">
                <div class="card">
                  <div class="btImg_bg">{{ lable.title }}</div>
                  <div class="chartContainer" v-if="curEquipIndex == index">
                    <Gauge
                      :key="curEquipIndex + '' + equip_id"
                      :index="curEquipIndex + '' + i"
                      :unit="lable.unit"
                      :val="lable.value"
                      :type="lable.title"
                    />
                    <!-- <p class="status">优</p> -->
                  </div>
                </div>
              </div>
              <div class="nodata" v-if="!equip.allinoneData.labels.length">暂无设备</div>
            </div>
          </transition-group>
        </div>
        <!-- bottom -->
        <div class="main_bottom">
          <div class="btImg_bg">最近24小时数据</div>
          <div class="chartLine">
            <Linechart :lineList="lineList" :lineChange="lineChange" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Left_menu from '../views/Left_menu.vue'
import Nav from '../views/Nav.vue'
import { getHosList, getEquipList, getLineData } from '@/utils/api'
import Gauge from '../views/gauge.vue'
import Linechart from '../views/linechart.vue'
import { parseTime } from '@/utils/index'
export default {
  name: 'home',
  components: {
    Left_menu,
    Nav,
    Gauge,
    Linechart
  },
  data() {
    return {
      HosList: [],
      hos_id: '',
      equip_id: '',
      equipList: [],
      curEquipIndex: 0,
      equipTimer: null,
      lineList: [],
      lineChange: true,
      loading: false,
      timer: null
    }
  },
  created() {
    if (window.localStorage.getItem('token')) {
      this.timer = setInterval(() => {
        this.initData()
      }, 300000)
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  watch: {
    curEquipIndex(val) {
      this.$refs.tagitems[val].scrollIntoView()
    },
    '$route.params.id'(val) {
      this.HosList = []
      this.equipList = []
      this.lineList = []
    }
  },
  methods: {
    //获取曲线数据
    getLineData() {
      let params = {
        end: parseTime(new Date()),
        start: parseTime(new Date().getTime() - 24 * 3600 * 1000),
        equip_id: this.equip_id,
        hos_id: this.hos_id
      }
      this.lineChange = false
      this.loading = true
      clearInterval(this.equipTimer)
      getLineData(params).then((res) => {
        this.lineChange = true
        this.lineList = res.data.line
        this.loading = false
        // this.startEqupTimer()
      })
    },

    // 点击获取当前设备
    getCurEquip(index, equip) {
      this.equip_id = equip.id
      this.curEquipIndex = index
      this.getLineData()
    },
    initData() {
      this.getHosList()
    },
    handleHosId(id) {
      this.hos_id = id
      this.getEquipList()
    },
    // 开始设备轮播
    startEqupTimer() {
      this.equipTimer = setInterval(() => {
        this.curEquipIndex++
        if (this.curEquipIndex == this.equipList.length) {
          this.curEquipIndex = 0
        }
        this.equip_id = this.equipList[this.curEquipIndex].id
        this.getLineData()
      }, 8000)
    },
    //获取设备列表
    getEquipList() {
      let params = {
        hos_id: this.hos_id,
        p_equip_type: 8
      }
      getEquipList(params).then((res) => {
        let list = []
        res.data.map((item) => {
          list = [...list, ...item.list]
        })
        this.equipList = list
        if (list.length) {
          this.equip_id = list[0].id
          this.curEquipIndex = 0
          this.getLineData()
        } else {
          this.lineList = []
          this.lineChange = !this.lineChange
        }
      })
    },
    async getHosList() {
      const { data: res } = await getHosList()
      this.HosList = res
      if (this.HosList.length) {
        this.hos_id = this.HosList[0].id
        this.getEquipList()
      }
    }
  },
  beforeDestroy() {
    clearInterval(this.equipTimer)
  }
}
</script>

<style lang="less">
.bg {
  width: 100%;
  height: 100%;
  // background: url(../assets/images/bg.png) no-repeat;
  background: #002a82;
  background-size: cover;
  background-position: center;
  // overflow-y: scroll;
}

// nav
.nav {
  background: url(../assets/images/nav1.png) no-repeat;
  position: relative;
  height: 74px;
  background-size: 100% 100%;

  h1 {
    // position:absolute;
    font-size: 0.42rem;
    top: 0.125rem;
    text-align: center;
    color: #206bfe;
    line-height: 74px;
    letter-spacing: 1px;
    text-shadow: 2px 2px 2px rgba(135, 159, 249, 0.3);
    // background: linear-gradient(180deg, #007af9 0%, #cbdcff 100%);
    background: #fff;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
}

.headright {
  position: absolute;
  top: 0.125rem;
  right: 0.25rem;
  display: flex;
  align-items: center;

  .user {
    position: absolute;
    top: 15px;
    right: 15px;
    display: flex;
    color: #027aff;
    font-size: 14px;
    line-height: 1.5;
    align-items: center;

    .userInfo {
      display: flex;
      align-items: center;

      .text {
        padding-left: 5px;
        color: #fff;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        align-items: center;

        .phone {
          color: #fff;
        }

        .logout {
          display: block;
          width: 104px;
          height: 32px;
          background: linear-gradient(135deg, #39bff6 0%, #2a38d4 100%);
          border-radius: 16px;
          text-align: center;
          line-height: 32px;
          cursor: pointer;
        }
      }
    }
  }

  img {
    width: 0.65rem;
    height: 0.65rem;
  }

  .user1 {
    color: #fff;
    font-size: 14px;
  }

  .logout1 {
    font-size: 14px;
    padding-left: 0.25rem;
    color: #4dfce5;
    cursor: pointer;
  }
}

.showTime {
  position: absolute;
  left: 0.375rem;
  top: 0;
  line-height: 0.9375rem;
  color: rgba(255, 255, 255, 1);
  font-size: 16px;
  font-weight: 600;
}

// 主页面
.main {
  // flex: 1;
  // width: calc(100% - 300px);
  width: 100%;
  padding: 0 20px;
}

.masker {
  position: fixed;
  z-index: 99;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.4);
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.popwindow {
  position: relative;
  padding: 40px 60px;
  background: hsla(0, 0%, 100%, 0.25);
  border-radius: 24px;
  border: 1px solid hsla(0, 0%, 100%, 0.7);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);

  h3 {
    color: #fff;
    font-size: 26px;
    font-weight: 400;
    margin-bottom: 40px;
    text-align: center;
  }
}

.closeIcon {
  font-size: 30px;
  color: #fff;
  position: absolute;
  top: 20px;
  right: 20px;
  cursor: pointer;
}

.el-form-item {
  margin-bottom: 18px;
}

.flexcontent {
  display: flex;
  width: 360px;

  .code {
    width: 150px;
    text-align: center;
    margin-left: 20px;
    height: 40px;
    background: #346fe7;
    border-radius: 6px;
    line-height: 40px;
    font-size: 20px;
    color: #fff;
    letter-spacing: 4px;
    cursor: pointer;
  }

  .inputbox {
    width: 360px;
    position: relative;

    input {
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      padding: 10px 40px;
      background: transparent;
      border-radius: 6px;
      border: 1px solid #ffffff;
      color: #fff;
    }

    :-ms-input-placeholder {
      color: #ddd;
    }

    ::-webkit-input-placeholder {
      color: #ddd;
    }
  }
}

.loginBtn {
  width: 360px !important;
  height: 50px !important;
  background: linear-gradient(135deg, #39bff6, #2a38d4) !important;
  border: none !important;
  color: #fff !important;
  border-radius: 6px !important;
  font-size: 20px !important;
  letter-spacing: 6px !important;
  margin-top: 20px !important;
}
.main_top {
  margin-top: 0.25rem;
  position: relative;
  height: calc(100vh - 364px);

  ul {
    white-space: nowrap;
    text-align: center;
    li {
      cursor: pointer;
      min-width: 200px;
      display: inline-block;
    }

    .allInOneList1 {
      margin: 0 0.25rem;
      width: 3.5375rem;
      height: 32px;
      background: linear-gradient(
        270deg,
        rgba(204, 204, 204, 0) 0%,
        rgba(204, 204, 204, 0.66) 50%,
        rgba(204, 204, 204, 0) 100%
      );
      text-align: center;
      line-height: 32px;
      font-size: 16px;
      color: #ccc;
    }

    .allInOneList2 {
      margin: 0 0.25rem;
      width: 3.5375rem;
      height: 32px;
      background: linear-gradient(
        270deg,
        rgba(244, 213, 35, 0) 0%,
        rgba(244, 213, 35, 0.66) 50%,
        rgba(245, 190, 28, 0) 100%
      );
      text-align: center;
      line-height: 32px;
      font-size: 16px;
      color: #ffae00;
    }
  }

  .ys_box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    position: absolute;
    left: 0;
    bottom: 0;
    right: 0;
    top: 48px;
  }
  .ys_box:after {
    content: '';
    width: 32.5%;
  }

  .ysList {
    width: 32.5%;
    /* 每个元素宽度为100% / 3 */
    height: 50%;
    padding-bottom: 16px;
    box-sizing: border-box;

    .card {
      background: #00319e80;
      width: 100%;
      height: 100%;

      .btImg_bg {
        background: url('../assets/images/title.png') no-repeat center;
        width: 100%;
        height: 30px;
        color: #fff;
        font-size: 16px;
        line-height: 30px;
        font-weight: 600;
        letter-spacing: 1px;
        text-align: center;
      }

      .chartContainer {
        width: 100%;
        height: calc(100% - 30px);
        margin: 0 auto;
        background: url(@/assets/images/gaugeBg.png) no-repeat bottom center;
        background-size: 60%;
        position: relative;
        .status {
          position: absolute;
          bottom: 10px;
          width: 100%;
          text-align: center;
          font-size: 20px;
          color: #a3a3a3;
          text-shadow: 2px 2px 2px rgba(135, 159, 249, 0.3);
          background: linear-gradient(90deg, #fffffb 0%, #7cbaf3 100%);
          -webkit-background-clip: text;
          -webkit-text-fill-color: transparent;
          font-weight: bold;
        }
      }
    }
  }
}

.main_bottom {
  height: 3.25rem;
  background: #00319e80;
}

.btImg_bg {
  background: url('../assets/images/title.png') no-repeat center;
  width: 100%;
  height: 30px;
  color: #fff;
  font-size: 16px;
  line-height: 30px;
  font-weight: 600;
  letter-spacing: 1px;
  text-align: center;
}

.chartLine {
  width: 100%;
  height: 100%;
}

.nodata {
  line-height: 200px;
  text-align: center;
  color: #fff;
  font-size: 16px;
  width: 100%;
}

/* 可以设置不同的进入和离开动画 */
/* 设置持续时间和动画函数 */
.slide-fade-enter-active {
  transition: all 0.3s ease;
}
.slide-fade-leave-active {
  transition: all 0.1s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active for below version 2.1.8 */ {
  transform: translateX(100%);
}

.outer {
  width: 100%;
  overflow-x: scroll;
}

/* 修改垂直滚动条 */
.outer::-webkit-scrollbar {
  height: 6px; /* 修改宽度 */
  border-radius: 5px;
}

/* 修改滚动条轨道背景色 */
.outer::-webkit-scrollbar-track {
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 5px;
}

/* 修改滚动条滑块颜色 */
.outer::-webkit-scrollbar-thumb {
  background-color: #10ffff;
  border-radius: 5px;
  display: none;
}
.outer:hover::-webkit-scrollbar-thumb {
  display: block;
}

/* 修改滚动条滑块悬停时的颜色 */
.outer::-webkit-scrollbar-thumb:hover {
  border-radius: 5px;
}
</style>